package org.visionlibrary.image.filters.effects;

import javax.media.jai.TiledImage;

import org.visionlibrary.image.acquisition.ImageFactory;
import org.visionlibrary.image.model.AbstractFilter;
import org.visionlibrary.image.model.FilterTools;
import org.visionlibrary.image.model.FilterTools.Complex;


public class FFTImageDevastator extends AbstractFilter {
	private int step;

	private float newPixelValue;

	public FFTImageDevastator(int step, float newPixelValue) {
		this.step = step;
		this.newPixelValue = newPixelValue;
	}

	public FFTImageDevastator() {
		this(5, 10.0f);
	}

	public TiledImage applyFilter(TiledImage src, TiledImage dest) {
		if (null == src)
			throw new NullPointerException("Source image is null.");

		if (null == dest)
			dest = (new ImageFactory()).createCompatibleImage(src);
		
		for (int ch = 0; ch < src.getNumBands(); ch++) {
			Complex[][] complexImage = FilterTools.getComplex(src, ch);
			Complex[][] fft = FilterTools.simpleFFT(complexImage);
			// FilterTools.Complex.revertQuarters( fft );
			//TODO: Sprawdzic o co mi w tym kodzie chodzilo
			int N = fft.length;
			int M = fft[0].length;

			for (int i = 0; i < N; i += step)
				for (int j = 0; j < M; j += step)
					fft[i][j] = new Complex(newPixelValue * (float) (i * j));

			// FilterTools.Complex.revertQuarters( fft );
			Complex[][] ifft = FilterTools.inverseFFT(fft);
			for (int x = 0; x < src.getWidth(); x++)
				for (int y = 0; y < src.getHeight(); y++)
					dest.setSample(x, y, ch, normalization
							.normalize((int) (ifft[y][x].abs())));
		}
		return dest;
	}
}
